<?php
namespace Common\Model;
use Think\Model;

class UserAddressModel extends Model{

	/**
	 *  获取用户一个地址
	 *  @param $where
	 */
	public function isUserAddress($userID,$addressID,$where=array()){
		$where['userID']=$userID;
		$where['addressID']=$addressID;
		$where['isOn']=1;
		$result=$this->where($where)->find(); 
		
		return $result;
	}
	
	/**
	 *  获取用户地址列表
	 *  @param $where
	 */
	public function getUserAddressList($userID,$page=1,$pageSize=15,$where=array()){
		$where['userID']=$userID;
		$where['isOn']=1;
		$result=$this->where($where)->page($page,$pageSize)->order("addressID desc")->select();  
		
		return $result;
	}
	
	/**
	 *  获取用户地址总数
	 *  @param $where
	 */
	public function getUserAddressCount($userID,$where=array()){
		$where['userID']=$userID;
		$where['isOn']=1;
		$result=$this->where($where)->count();
	
		return $result;
	}
	
	/**
	 *  添加一个用户地址
	 *  @param $where
	 */
	public function addAddress($data){
		$data['addTime']=time();
		$result=$this->data($data)->add();
		
		return $result;
	}
	
	/**
	 *  编辑一个用户地址
	 *  @param $where
	 */
	public function editAddress($data){
		$data['updateTime']=time();
		$result=$this->save($data);
	
		return $result;
	}
	
	/**
	 *  编辑一个用户地址
	 *  @param $where
	 */
	public function deleteAddress($where){
		$data['isOn']=0;
		$result=$this->where($where)->save($data);
	
		return $result;
	}
	
	/**
	 *  删除用户所有默认地址
	 *  @param $data
	 */
	public function noDefaultAddress($userID){
		$data['updateTime']=time();
		$data['isDefault']=0;
		$where['userID']=$userID;
		$result=$this->where($where)->save($data);
	
		return $result;
	}
	
}
?>